﻿var showHTMLCtl;
if (!showHTMLCtl) showHTMLCtl = {};
if (!showHTMLCtl.prototype) showHTMLCtl.prototype = {};

window._isIE = (navigator.appName == "Microsoft Internet Explorer");
if (window._isIE) {
    if (navigator.userAgent.indexOf("Opera") > -1) window._isIE = null;
    if (navigator.userAgent.indexOf("Windows NT 6.0") > -1) window._isVista = true;
    else window._isVista = false;
}
else {
    if (navigator.userAgent.indexOf("Gecko") == -1) window._isIE = null;
}
var IsIe = (window.ActiveXObject) ? true : false;

function showHTMLCtl(name) { //初始化属性
    this.name = name;      //对象名称
}
//
showHTMLCtl.prototype.$ = function(_sid) {
    return document.getElementById(_sid);
}
showHTMLCtl.prototype.$$ = function(ctl_name) {
    return document.getElementsByName(ctl_name);
}
showHTMLCtl.prototype.$$$ = function(ctl_tag, ctl_id) {
    if (ctl_id != null) {
        return this.$(ctl_id).getElementsByTagName(ctl_tag);
    }
    else {
        return document.getElementsByTagName(ctl_tag);
    }
}
String.prototype.len=function(){ 
    return this.replace(/[^\x00-\xff]/g,"**").length; 
}

showHTMLCtl.prototype.GetLen=function(str_){ 
    return str_.replace(/[^\x00-\xff]/g,"**").length; 
}

showHTMLCtl.prototype.Eval = function(code) {
    if (!!(window.attachEvent && !window.opera)) {
        //ie
        execScript(code);
    } else {
        //not ie
        window.eval(code);
    }
}
//取得焦点
showHTMLCtl.prototype.focus = function(_sid) {
    this.$(_sid).focus();
}

showHTMLCtl.prototype.GetValueOrTextBySelectID = function(SelectID, ValueOrText) {
    var rvl = null;

    var type_ = ValueOrText.toString().toLowerCase();
    var Select = this.$(SelectID);
    if (Select) {
        try {
            rvl = eval("Select.options[Select.selectedIndex]." + type_);
        } catch (e) { }
    }
    return rvl;
}
showHTMLCtl.prototype.GetValueByRadioName = function(RadioName) {
    var rvl = null;
    var Radiolist = this.$$(RadioName);
    if (Radiolist.length) {
        for (var i = 0; i < Radiolist.length; i++) {
            if (Radiolist[i].checked) {
                rvl = Radiolist[i].value;
                break;
            }
        }
    }
    return rvl;
}
//返回小写
showHTMLCtl.prototype.GetToLowerCase = function(Str_) {
    return Str_.toLowerCase();
}
showHTMLCtl.prototype.GetRound = function() {
    var i_N =Math.random();// Math.round(Math.random() * 9999); //0-9999
    return i_N;
}
showHTMLCtl.prototype.ImgOverOut = function(obj_, type) {
    if (IsIe) {
        if (obj_.filters.alpha == undefined) {
            obj_.style.filter = "alpha(opacity=0)";
        }
        if (type == 1) {
            obj_.filters.alpha.opacity = parseInt(30);
        }
        else {
            obj_.filters.alpha.opacity = parseInt(100);
        }
    } else {
        if (type == 1) {
            obj_.style.opacity = 0.3;
        }
        else {
            obj_.style.opacity = 1;
        }
    }
}

//返回指定控件的样式名
showHTMLCtl.prototype.GetClassByID = function(cid_) {
    return this.$(cid_).className;
}
//设置指定控件新的样式值
showHTMLCtl.prototype.SetClassByID = function(cid_, nclass_) {
    return this.$(cid_).className = nclass_;
}
//设置指定控件的样式值
showHTMLCtl.prototype.DelClassByID = function(cid_, nclass_) {
    return this.$(cid_).className = this.$(cid_).className.replace(new RegExp("\\s*\\b" + nclass_ + "\\b", "g"), "");
}

//返回指定控件的属性值
showHTMLCtl.prototype.GetAttributeByID = function(cid_, AttributeName) {
    return this.$(cid_).getAttribute(AttributeName);
}
//设置指定控件属性的新值
showHTMLCtl.prototype.SetAttributeByID = function(cid_, AttributeName, nAttributeNameValue) {
    return this.$(cid_).setAttribute(AttributeName, nAttributeNameValue);
}


//选中或取消复选框
showHTMLCtl.prototype.CheckBoxAllByName = function(Name_, Type_) {
    for (var i = 0; i < this.$$(Name_).length; i++) {
        var e = this.$$(Name_)[i];
        e.checked = Type_;
    }
}
//返回指定控件的值
showHTMLCtl.prototype.GetValueByID = function(cid_) {
    return this.$(cid_).value;
}
//设置指定控件的值
showHTMLCtl.prototype.SetValueByID = function(cid_, nvalue_) {
    return this.$(cid_).value = nvalue_;
}

//返回指定控件的html
showHTMLCtl.prototype.GetHtmlByID = function(cid_) {
    return this.$(cid_).innerHTML;
}
//设置指定控件的html值
showHTMLCtl.prototype.SetHtmlByID = function(cid_, nhtml_) {
    return this.$(cid_).innerHTML = nhtml_;
}

//js读取和写入cookie
//根据cookie名称返回cookie值
showHTMLCtl.prototype.readCookie = function(name) {
    var cookieValue = "";
    var search = name + "=";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
            cookieValue = unescape(document.cookie.substring(offset, end));
        }
    }
    return cookieValue;
}

//name为cookie名称,value为cookie值,hours为cookie的有效时长
showHTMLCtl.prototype.writeCookie = function(name, value, hours) {
    var expire = "";
    if (hours != null) {
        expire = new Date((new Date()).getTime() + hours * 3600000);
        expire = "; expires=" + expire.toGMTString();
    }
    document.cookie = name + "=" + escape(value) + expire + ";path=/";
}
//js读取和写入cookie

//获取页面ID参数
showHTMLCtl.prototype.Request = function(name) {
    var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
    if (reg.test(location.href))
        return unescape(RegExp.$2.replace(/\+/g, " "));
    return "";
}

//隐藏select控件
showHTMLCtl.prototype.HideSelect = function() {
    var selects = this.$$$('select',null);
    for (i = 0; i < selects.length; i++) {
        selects[i].style.visibility = 'hidden';
    }
}

//显示或隐藏页面上所有的select控件
showHTMLCtl.prototype.ShowSelect = function() {
    var list_ = this.$$$("select",null);
    for (var i = 0; i < list_.length; i++) {
        list_[i].style.visibility = 'visible';
    }
}

//验证emial地址
showHTMLCtl.prototype.ChekEmail = function(temail) {
    var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
    var eemail_2 = temail.split('.');

    if (pattern.test(temail) && eemail_2.length > 1 && eemail_2[1].length > 0) {
        return true;
    }
    else {
        return false;
    }
}

showHTMLCtl.prototype.Replace = function(ReplaceStr, _OldStr, _NewStr) {
    //替换所有的_OldStr
    var reg = new RegExp("(" + _OldStr + ")", "g");
    ReplaceStr = ReplaceStr.replace(reg, _NewStr);
    return ReplaceStr;
}
//替换掉所有过多的换行和回车
showHTMLCtl.prototype.Replace2 = function(ReplaceStr) {
    reg = new RegExp("\r\n", "g");
    reg2 = new RegExp("\n", "g");
    reg3 = new RegExp(" ", "g");

    reg4 = new RegExp("\r\n\r\n", "g");
    reg5 = new RegExp("  ", "g");

    ReplaceStr = ReplaceStr.replace(reg4, '');
    ReplaceStr = ReplaceStr.replace(reg5, '');

    ReplaceStr = ReplaceStr.replace(reg, '<br>');
    ReplaceStr = ReplaceStr.replace(reg2, '<br>');
    ReplaceStr = ReplaceStr.replace(reg3, '&nbsp;');

    if (ReplaceStr == " " || ReplaceStr == "<br>" || ReplaceStr == "&nbsp;") {
        return "";
    }
    return ReplaceStr;
}

//替换掉所有过多的换行和回车
showHTMLCtl.prototype.Replace3 = function(ReplaceStr, obj) {
//obj参数如：
//[{ indexStr: "\r\n\r\n", newStr: "\r\n" },{ indexStr: "\r\n", newStr: " "},{indexStr: "  ", newStr: ""}]

    if (obj.length > 0) {
        var reg;
        for (var i = 0; i < obj.length; i++) {
            reg = new RegExp(obj[i].indexStr, "g");
            ReplaceStr = ReplaceStr.replace(reg, obj[i].newStr);
        }
    }

    if (ReplaceStr == " " || ReplaceStr == "<br>" || ReplaceStr == "&nbsp;") {
        return "";
    }
    return ReplaceStr;
}

//添加事件,element为对象,eventType为事件,handler事件发生时执行的代码
showHTMLCtl.prototype.addEventListener = function(element, eventType, handler) {
    try {
        if (element.addEventListener)
            element.addEventListener(eventType, handler, false);
        else if (element.attachEvent)
            element.attachEvent("on" + eventType, handler);
    }
    catch (e) { }
};
//hbctl_.addPanelEventListeners(this.$(qq), "click", function(e) { return self.onTabClick(e, tab); });
//hbctl_.addEventListeners(hbctl_.$("f_nc0"), "focus", function(e) { hbctl_.SetHtmlByID("tttt", "1111111111111111"); });

showHTMLCtl.prototype.removeEventListener = function(element, eventType, handler) {
    try {
        if (element.removeEventListener)
            element.removeEventListener(eventType, handler, false);
        else if (element.detachEvent)
            element.detachEvent("on" + eventType, handler);
    }
    catch (e) { }
};
/*使用方法
function handleClick() {
alert("Click");
var oDiv = document.getElementById("div1");
hbctl_.removeEventHandler(oDiv, "click", handleClick);
}
window.onload = function() {
var oDiv = document.getElementById("div1");
hbctl_.addEventHandler(oDiv, "click", handleClick);
}
*/


/*
参数介绍：
where：插入位置。包括beforeBegin,beforeEnd,afterBegin,afterEnd。
el：用于参照插入位置的html元素对象
html：要插入的html代码*/

//<script type="text/javascript">
showHTMLCtl.prototype.insertHtml = function(where, el, html) {
    where = where.toLowerCase();
    if (el.insertAdjacentHTML) {
        switch (where) {
            case "beforebegin":
                el.insertAdjacentHTML('BeforeBegin', html);
                return el.previousSibling;
            case "afterbegin":
                el.insertAdjacentHTML('AfterBegin', html);
                return el.firstChild;
            case "beforeend":
                el.insertAdjacentHTML('BeforeEnd', html);
                return el.lastChild;
            case "afterend":
                el.insertAdjacentHTML('AfterEnd', html);
                return el.nextSibling;
        }
        throw 'Illegal insertion point -> "' + where + '"';
    }
    var range = el.ownerDocument.createRange();
    var frag;
    switch (where) {
        case "beforebegin":
            range.setStartBefore(el);
            frag = range.createContextualFragment(html);
            el.parentNode.insertBefore(frag, el);
            return el.previousSibling;
        case "afterbegin":
            if (el.firstChild) {
                range.setStartBefore(el.firstChild);
                frag = range.createContextualFragment(html);
                el.insertBefore(frag, el.firstChild);
                return el.firstChild;
            } else {
                el.innerHTML = html;
                return el.firstChild;
            }
        case "beforeend":
            if (el.lastChild) {
                range.setStartAfter(el.lastChild);
                frag = range.createContextualFragment(html);
                el.appendChild(frag);
                return el.lastChild;
            } else {
                el.innerHTML = html;
                return el.lastChild;
            }
        case "afterend":
            range.setStartAfter(el);
            frag = range.createContextualFragment(html);
            el.parentNode.insertBefore(frag, el.nextSibling);
            return el.nextSibling;
    }
    throw 'Illegal insertion point -> "' + where + '"';
}
//insertHtml("beforeEnd", hbctl_.$('list_content'), temp_str);
//</script>

showHTMLCtl.prototype.GetMaxLength=function (strText,length,isCut) 
{
	var tempString=strText;
	
	if(this.GetLen(strText)<=length)
	{
	    return strText;
	}
	else
	{
	    var tt=""; 
	    for(var i=0;i<length;i++) 
		    { 
			    if(tt.len()<length) 
				    tt=tempString.substr(0,i+1); 
			    else 
				    break; 
		    } 
	    if(tt.len()>length)
		    tt=tt.substr(0,tt.length-1);
	    return isCut? tt+"...":tt;
	}
}
//设为首页
showHTMLCtl.prototype.SetHomePage = function(homepageurl) {
    document.body.style.behavior = 'url(#default#homepage)';
    if (!(document.body.isHomePage(homepageurl))) {
        document.body.sethomepage(homepageurl);
    }
}
//加入收藏夹
showHTMLCtl.prototype.AddFavorite = function(newtitle_, weburl_) {
    if (document.all) {
        window.external.addFavorite(weburl_, newtitle_);
    }
    else if (window.sidebar) {
        window.sidebar.addPanel(newtitle_, weburl_, "");
    }
}

var hbctl_ = new showHTMLCtl("hbctl_");